<template>
  <div class="user">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--  -->
    <el-card class="box-card" style="margin-top: 15px">
      <!--  -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            @input="searchUser"
            @clear="searchUser"
            clearable
            class="input-with-select"
          >
            <el-button slot="append" icon="el-icon-search"  @click="searchUser"></el-button>
          </el-input>
        </el-col>
        <el-col :span="16"
          ><el-button type="primary"  @click="dialogFormVisible = true">添加用户</el-button></el-col
        >
      </el-row>
      <!--  -->
      <el-table :data="list" border style="width: 100%; margin-top: 15px">
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="username" label="姓名"> </el-table-column>
        <el-table-column prop="email" label="邮箱"> </el-table-column>
        <el-table-column prop="mobile" label="电话"> </el-table-column>
        <el-table-column prop="role_name" label="角色" width="100">
        </el-table-column>
        <el-table-column label="状态" width="70">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.mg_state"  @change="changestate(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
               @click="edit(scope.row)"
            ></el-button>
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="del(scope.row.id)"
            ></el-button>
            <el-button
              type="warning"
              size="mini"
              icon="el-icon-setting"
              @click="setUserRoleDia(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--  -->
      <el-pagination
        style="margin-top: 15px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 11, 12]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!--  -->
    <el-dialog title="添加用户" :visible.sync="dialogFormVisible">
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    
    <el-form-item label="用户名" prop="username">
    <el-input v-model="ruleForm.username"></el-input>
  </el-form-item>
  <el-form-item label="密码" prop="password">
    <el-input v-model="ruleForm.password"></el-input>
  </el-form-item>
  <el-form-item label="邮箱" prop="email">
    <el-input v-model="ruleForm.email"></el-input>
  </el-form-item>
  <el-form-item label="电话" prop="mobile">
    <el-input v-model="ruleForm.mobile"></el-input>
  </el-form-item>
    
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
  </div>
</el-dialog>
<!--  -->
<el-dialog title="编辑用户" :visible.sync="dialogFormVisiblebj">
      <!--  -->
      <el-form
        :model="ruleFormbj"
        :rules="rulesbj"
        ref="ruleFormbj"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" >
          <el-input v-model="ruleFormbj.username" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="ruleFormbj.email"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="mobile">
          <el-input v-model="ruleFormbj.mobile"></el-input>
        </el-form-item>
      </el-form>
      <!--  -->
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisiblebj = false">取 消</el-button>
        <el-button type="primary" @click="submitFormbj('ruleFormbj')">确 定</el-button>
      </div>
    </el-dialog>
    <!--  -->
   <el-dialog
      title="分配角色"
      :visible.sync="isSetUserRoleDia"
      width="36%"
    >
      
      <div>
        <p>当前用户：<span>{{ currentUser.username }}</span></p>
        <p>当前角色：<span>{{ currentUser.role_name }}</span></p>
        <span class="mr10">分配新角色:</span>
          <el-select v-model="currentRoleId" placeholder="请选择">
              <el-option
                v-for="item in rolesList"
                :key="item.id"
                :label="item.roleName"
                :value="item.id">
              </el-option>
        </el-select>
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button @click="isSetUserRoleDia = false">取 消</el-button>
        <el-button type="primary" @click="setUserRoleOK"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import _ from "lodash";
import {getRoleListAPI} from "@/http/api"
import {setUserRoleAPI} from "@/http/api"
import {usserEditApi} from "@/http/api"
import {usserStateApi} from "@/http/api"
import {usserDelApi} from "@/http/api"
import {usserAddApi} from "@/http/api"
import { userApi } from "@/http/api";
export default {
  data() {
    return {
      search: "",
      list: [],
      value: false,
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 3,
      },
      total: 0,
      dialogFormVisible: false,
      formLabelWidth: '120px',
       ruleForm: {
          username: '',
          password: '',
          email: '',
          mobile: '',
          type: [],
        },
        rules:{
          // username: [
          //   { required: true, message: '请输入用户名', trigger: 'blur' },
          //   { min: 3, max: 7, message: '长度在 3 到 7 个字符', trigger: 'blur' }
          // ],
          //  password: [
          //   { required: true, message: '请输入密码', trigger: 'blur' },
          //   { min: 3, max: 7, message: '长度在 3 到 7 个字符', trigger: 'blur' }
          // ],
          //  email: [
          //   { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          //   { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
          // ],
          // mobile: [
          //   { required: true, message: '请输入电话', trigger: 'blur' },
          //    {pattern:/^1[3456789]\d{9}$/, message: '11位正确号码', trigger: 'blur' }
          // ],
        },
        dialogFormVisiblebj:false,
        ruleFormbj: {
          username: '',
          email:"",
          mobile:""
        },
         rulesbj:{
          //  email: [
          //   { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          //   { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
          // ],
          // mobile: [
          //   { required: true, message: '请输入电话', trigger: 'blur' },
          //   {pattern:/^1[3456789]\d{9}$/, message: '11位正确号码', trigger: 'blur' }
          // ],
        },
        currentRoleId:'',
      rolesList:[],
      isSetUserRoleDia:false,
      isEditUserDia: false,
       currentUserId: 0,
      currentUser:{}
    };
  },
  created() {
    this.getlist();
  },
  components: {},
  mounted() {},
  methods: {
    setUserRoleOK() {

      setUserRoleAPI(this.currentUserId,this.currentRoleId).then(res=>{
        // console.log('qqq',res);
      })
      this.isSetUserRoleDia=false
      this.getlist()
    },
   
   async setUserRoleDia(row) {
     
      this.currentRoleId=''
      const {id,username,role_name}=row
      this.currentUserId=id
      this.currentUser={username,role_name}
      const res=await getRoleListAPI()
      this.rolesList=res;
      // console.log('aaa',res);

      this.isSetUserRoleDia=true
    },
     searchUser: _.debounce(function () {
      this.getlist();
    }, 300),
     submitFormbj(formName) {  
        this.$refs[formName].validate((valid) => {
          if (valid) {
            usserEditApi(this.ruleFormbj).then(res=>{
              console.log(res);
               this.dialogFormVisiblebj=false
               this.getlist()
            })
            console.log('qweqwewqe',this.ruleFormbj);
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
    edit(row){
        this.dialogFormVisiblebj=true
         this.ruleFormbj=row
    },
    changestate(row){
      console.log(row);
      usserStateApi(row.id,row.mg_state).then(res=>{
       
      })
    },
    del(id){
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          usserDelApi(id).then(res=>{
        console.log(res);
        this.getlist()
      })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        })
    },
    submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            
          for (let index = 0; index < 10; index++) {
            this.ruleForm.username= `${index}`
            usserAddApi(this.ruleForm).then(res=>{
             console.log(res);
            this.dialogFormVisible=false
             this.getlist()
           })
          }
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
    async getlist() {
      const res = await userApi(this.queryInfo);
      console.log(res);
      let { total, users } = res;
      this.list = users;
      this.total = total;
    },
    handleSizeChange(val) {
      this.queryInfo.pagesize = val;
      this.getlist();
    },
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val;
      this.getlist();
    },
  },
  computed: {},
  watch: {},
};
</script>

<style scoped lang='scss'>

</style>
